<template>
  <view class="content">
    <view   style="text-align:center;font-size: large;font-weight: bold">销售订单汇总</view>
    <view class="container">
      <button type="default"  @click="orderbyChange">添加查询条件</button>
      <button  type="default"   @click="excelExport('exportXls')">导出列表数据</button>
    </view>
    <view class="container">
      <button @click="excelExport('exportFJMXXls2')">一级分类分拣</button>
      <button @click="excelExport('exportFJMXXls4')">二级分类分拣</button>
      <button @click="excelExport('exportFJMXXls3')">供应商分拣</button>
    </view>

    <uni-card title="全部功能" >
      <view style="height: 700px">
        <zb-table
            :show-header="true"
            :columns="column"
            :stripe="true"
            :fit="false"
            show-summary
            sum-text="合计"
            :cell-style="cellStyle"
            :summary-method="getSummaries"
            @toggleRowSelection="toggleRowSelection"
            :highlight="true"
            @toggleAllSelection="toggleAllSelection"
            :border="true"
            @edit="buttonEdit"
            @dele="dele"
            :data="data">

        </zb-table>
      </view>
    </uni-card>


    <fjj-condition ref='condition' @touchmove.stop :color="color" :list="menuList" :defaultValue="defaultValue" @result="resultConditon" />
  </view>
</template>
<script>
const app = getApp();
import fjjCondition from '@/components/fjj-condition/fjj-condition.vue';
export default {
  data() {
    return {
      user: null,
      color: '#4D7BFE',
      hasChoose: false,
      menuList: [],
      defaultValue: {},
      obj:{},

      column:[
        // { type:'selection', fixed:true,width:50 },
        { name: 'billDate', label: '日期',sorter:true,fixed:true },
        { name: 'name', label: '物料名称',sorter:true ,fixed:true},
        { name: 'qty', label: '订单数量',sorter:true ,width:90},
        { name: 'unitId_dictText', label: '单位',sorter:true,width:60 },
        { name: 'customerId_dictText', label: '客户',sorter:true,fixed:true,width:160,emptyString:'--' },

        { name: 'model', label: '型号',sorter:true },
      ],
      data:[]
    };
  },
  components: {
    fjjCondition,
  },
  onLoad() {
    this.user =app.globalData.get_user_cache_info();
    setTimeout(() => {
      this.menuList = [
        {
          'title': '日期范围',
          'type': 'range',
          'key': 'time1',
          'minName': 'time1MinName',
          'maxName': 'time1MaxName',
        }
      ]

      this.getCurrentTime();
      // 数据加载
      let obj=  {
        'title': '客户(可多选)',
        'type': 'custom',
        'key': 'customer',
        'isMutiple': true, //多选
        'detailList': [

        ],
      };
      //发送请求获取客户数据
      uni.request({
        url: app.globalData.get_request_url_full("bas/basCustomer/list"),
        method: 'GET',
        data: {
          pageSize:100
        },
        header:{
          'Tenant-Id':this.user.role.tenantId,
          'X-Access-Token':this.user.token,
        },
        dataType: 'json',
        success: res => {
          console.log("rescccccccccccc",res);
          // uni.hideLoading();
          // this.data=res.data.result.records;
          let values=res.data.result.records;

          for(let val of values){
            let ob= {
              title: val.name,
              value: val.id
            };
            obj.detailList.push(ob);
          }
          this.menuList.push(obj);
        },
        fail: () => {
          // app.globalData.showToast('服务器请求出错');
        }
      });


    },1000)

  },




  methods: {

    currentChange(row,index){
      uni.showToast({
        icon:'none',
        duration:3000,
        title:'选中当前一行'
      })
      console.log('单选',row,index)
    },
    getCurrentTime() {
      const now = new Date();
      const year = now.getFullYear();
      let month = now.getMonth() + 1;
      let day = now.getDate();
      // const hour = now.getHours();
      // const minute = now.getMinutes();
      // const second = now.getSeconds();
      if(month< 10) {
        month = "0" + month;
      }
      if(day< 10){
        day ="0" + day;
      }
      let todayTime = `${year}-${month}-${day}`;
      this.defaultValue.time1=[todayTime,todayTime];
    },
    //筛选返回值
    resultConditon(obj) {
      this.$refs.condition.visibleDrawer = false;
      this.hasChoose = obj.hasChoose;
      this.obj=obj;
      console.log("obj",obj);
      //发送请求
      // 加载loding
      uni.showLoading({
        title: '加载中...',
        mask: true
      });
      let customerId="";
      for(let val of obj.result.customer){
        if(customerId===""){
          customerId=val;
        }else {
          customerId=customerId+','+val;
        }

      }
      uni.request({
        url: app.globalData.get_request_url_full("sal/salOrderEntryTotalLv/list"),
        method: 'GET',
        data: {
          billDate_begin: obj.result.time1MinName,
          billDate_end: obj.result.time1MaxName,
          pageSize:1000,
          customerId:customerId
        },
        header:{
          'Tenant-Id':this.user.role.tenantId,
          'X-Access-Token':this.user.token,
        },
        dataType: 'json',
        success: res => {
          console.log("res",res);
          uni.hideLoading();
          this.data=res.data.result.records;
          console.log("this.datea",this.data);
        },
        fail: () => {
          // app.globalData.showToast('服务器请求出错');
        }
      });


    },
    orderbyChange(obj) {
      this.$refs.condition.visibleDrawer = true;
    },
    excelExport(url) {
      //发送请求
      // 加载loding
      let obj=this.obj;
      uni.showLoading({
        title: '加载中...',
        mask: true
      });
      let customerId="";
      for(let val of obj.result.customer){
        if(customerId===""){
          customerId=val;
        }else {
          customerId=customerId+','+val;
        }

      }

      uni.request({
        url: app.globalData.get_request_url_full("sal/salOrderEntryTotalLv/"+url),
        method: 'GET',
        data: {
          billDate_begin: obj.result.time1MinName,
          billDate_end: obj.result.time1MaxName,
          pageSize:1000,
          customerId:customerId,
          appFlag:'Y'
        },
        header:{
          'Tenant-Id':this.user.role.tenantId,
          'X-Access-Token':this.user.token,
        },
        responseType:'arraybuffer',
        success: res => {
          console.log("res",res);
          uni.hideLoading();
          const url = app.globalData.get_request_url_full("sys/common/static/temp/"+this.user.role.tenantId+".xls");
          uni.downloadFile({
            url,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: (res) => {
              const filePath = res.tempFilePath;
              uni.openDocument({
                filePath: filePath,
                showMenu: true,
                success: function (res) {
                  console.log('打开文档成功');
                }
              });
            },
            fail: err => {
              console.log(url);
              console.log('读取失败');
            }
          });
        },
        fail: () => {
       //   app.globalData.showToast('服务器请求出错');
        }
      });

    }
  }
};
</script>


<style>
.container {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
}
</style>
